<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

/**
 * AIR2 Discriminator Utils
 *
 * Static class containing functions used by the Discriminator to log activity
 * and move Sources into Organizations.
 *
 * @author rcavis
 * @package default
 */
class DiscUtils {

    /**
     * Opt a Source into an Organization, setting that as their Home
     * Organization if they are a new Source.
     *
     * @param Source  $src
     * @param Tank    $tank
     */
    public static function src_opt_in(Source $src, Tank $tank) {
        if (!$tank->tank_org_id) return;

        // check for existing
        $q = Doctrine_Query::create()->from('SrcOrg so');
        $q->addWhere('so.so_src_id = ?', $src->src_id);
        $q->addWhere('so.so_org_id = ?', $tank->tank_org_id);
        $so = $q->fetchOne();
        $q->free();

        // create the relation if DNE
        if (!$so) {
            $so = new SrcOrg();
            $so->so_src_id = $src->src_id;
            $so->so_org_id = $tank->tank_org_id;
            $so->so_effective_date = air2_date();

            // check if this should be home
            $conn = AIR2_DBManager::get_master_connection();
            $q_count = 'select count(*) from src_org where so_src_id = ?';
            $num = $conn->fetchOne($q_count, array($src->src_id), 0);
            $so->so_home_flag = ($num < 1);
        }
        $so->so_status = SrcOrg::$STATUS_OPTED_IN;
        $so->save();
        $so->free();
    }


    /**
     * Log an import activity to the SrcActivity log.
     *
     * @param Source  $src
     * @param Tank    $tank
     */
    public static function src_log_activity(Source $src, Tank $tank) {
        if (!$tank->tank_actm_id) return;

        $sa = new SrcActivity();
        $sa->sact_actm_id = $tank->tank_actm_id;
        $sa->sact_src_id = $src->src_id;
        $sa->sact_prj_id = $tank->tank_prj_id;
        $sa->sact_dtim = $tank->tank_dtim;
        $sa->sact_notes = $tank->tank_notes;
        $sa->sact_xid = $tank->tank_id;
        $sa->sact_ref_type = SrcActivity::$REF_TYPE_TANK;

        // description based on type
        if ($tank->tank_type == Tank::$TYPE_CSV) {
            $sa->sact_desc = 'Source data uploaded with CSV';
        }
        else {
            $sa->sact_desc = 'Source data imported';
        }

        // pull cre user from the tank
        $sa->sact_cre_user = $tank->tank_user_id;
        $sa->sact_cre_dtim = air2_date();
        $sa->sact_upd_user = $tank->tank_user_id;
        $sa->sact_upd_dtim = air2_date();

        $sa->save();
        $sa->free();
    }


}
